package com.qiangzhi.controller.admin;

import com.qiangzhi.pojo.dto.UserDTO;
import com.qiangzhi.pojo.dto.UserPageQueryDTO;
import com.qiangzhi.pojo.vo.UserVO;
import com.qiangzhi.result.PageResult;
import com.qiangzhi.result.Result;
import com.qiangzhi.service.ManageService;
import com.qiangzhi.utils.ValidateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 导入教师和学生账号相关接口
 */
@RestController
@RequestMapping("/admin/manage")
@Api(tags = "导入教师和学生账号相关接口")
@Slf4j
public class ManageController {

    @Autowired
    private ManageService manageService;

    /**
     * 新增教师和学生账号
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/insertUser")
    @ApiOperation("新增教师和学生账号")
    public Result insertUser(@Validated @RequestBody UserDTO userDTO) {
        log.info("新增：{}", userDTO);
        manageService.insertUser(userDTO);
        return Result.success();
    }

    /**
     * 批量新增教师和学生账号
     *
     * @param excelFile
     * @return
     */
    @PostMapping("/insertBatch")
    @ApiOperation("批量新增教师和学生账号")
    public Result insertBatch(MultipartFile excelFile) throws IOException {
        ValidateUtil.validateExcelFile(excelFile); // 新增操作前对文件进行校验
        log.info("批量新增：{}", excelFile);
        manageService.insertBatch(excelFile);
        return Result.success();
    }

    /**
     * 教师和学生分页查询
     *
     * @param userPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("教师和学生分页查询")
    public Result<PageResult> page(UserPageQueryDTO userPageQueryDTO) {
        log.info("分页查询：{}", userPageQueryDTO);
        PageResult pageResult = manageService.page(userPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询教师和学生信息
     *
     * @param id
     * @param identify
     * @return
     */
    @GetMapping("/{id}/{identify}")
    @ApiOperation("根据id查询教师和学生信息")
    public Result<UserVO> getById(@PathVariable Long id, @PathVariable String identify) {
        log.info("查询信息：{},{}", id, identify);
        UserVO userVO = manageService.getById(id, identify);
        return Result.success(userVO);
    }

    /**
     * 根据id删除教师和学生信息
     *
     * @param id
     * @param identify
     * @return
     */
    @DeleteMapping("/deleteUser")
    @ApiOperation("根据id删除教师和学生信息")
    public Result deleteById(@RequestParam Long id, @RequestParam String identify) {
        log.info("删除用户：{}", id);
        manageService.deleteById(id, identify);
        return Result.success();
    }

    /**
     * 根据id编辑教师和学生信息
     *
     * @param userDTO
     * @return
     */
    @PutMapping("/updateUser")
    @ApiOperation("编辑教师和学生信息")
    public Result updateUser(@Validated @RequestBody UserDTO userDTO) {
        log.info("编辑信息：{}", userDTO);
        manageService.updateUser(userDTO);
        return Result.success();
    }

    /**
     * 批量禁用教师和学生账号
     *
     * @param excelFile
     * @return
     */
    @PostMapping("/updateBatch")
    @ApiOperation("批量禁用教师和学生账号")
    public Result updateBatch(MultipartFile excelFile) throws IOException {
        ValidateUtil.validateExcelFile(excelFile); // 新增操作前对文件进行校验
        log.info("批量禁用：{}", excelFile);
        manageService.updateBatch(excelFile);
        return Result.success();
    }

}
